package controllers;

import play.*;
import play.mvc.*;

import java.util.*;

import models.*;

import play.data.validation.*;

public class Clients extends Controller
{
	public static void index()
	{
		List<Client> clients = Client.findAll();
	
		render(clients);
	}

	public static void show(Long id)
	{
		Client client = Client.findById(id);
		
		List<ClientOrder> orders = ClientOrder.find("byClient", client).fetch();
	
		render(client, orders);		
    }
	
	public static void create()
	{
		List<Neighborhood> neighborhoods = Neighborhood.findAll();
	
		render(neighborhoods);
	}
	
	public static void save(@Valid Client client, Long neighborhoodId)
	{
		/*if(validation.hasErrors())
		{
			params.flash();
			validation.keep();
			create();
		}*/
		
		Neighborhood neighborhood = Neighborhood.findById(neighborhoodId);
		
		client.neighborhood = neighborhood;
	
		client.save();
		
		show(client.id);
	}
	
	public static void update(Long id)
	{
		Client client = Client.findById(id);
		
		List<Neighborhood> neighborhoods = Neighborhood.findAll();
		
		render(client, neighborhoods);
	}
	
	public static void doUpdate(@Valid Client client, Long neighborhoodId)
	{
		/*if(validation.hasErrors())
		{
			params.flash();
			validation.keep();
			update(client.id);
		}*/
		Neighborhood neighborhood = Neighborhood.findById(neighborhoodId);
		
		client.neighborhood = neighborhood;
	
		client.save();
		
		show(client.id);
	}
	
	public static void delete(Long id)
	{
		Client client = Client.findById(id);
		
		client.delete();
		
		index();
	}
}